# @Time:   2021/1/6 15:56
# @Author: Zong Wei

import json
import os

import xmltodict


class XmlToJson:
    @staticmethod
    # 定义xml转json的函数
    def xml_to_json(file="testxml.xml"):
        path = os.path.split(os.path.dirname(__file__))[0]+'/data/'
        file_path = os.path.join(path, file)

        with open(file_path, 'r') as fb:
            xml_str = fb.read()
            xml_parse = xmltodict.parse(xml_str)

        json_str = json.dumps(xml_parse, indent=3, ensure_ascii=False)
        print(json_str)
        return json_str

    # @staticmethod
    # # json转xml函数
    # def json_to_xml():
    #     # xmltodict库的unparse()json转xml
    #     # 参数pretty 是格式化xml
    #     xml_str = xmltodict.unparse(json_str, pretty=1)
    #     print(xml_str)
    #     return xml_str


if __name__ == '__main__':
    XmlToJson.xml_to_json()


